package com.mybatis.dao;

import com.mybatis.domain.User;
import com.mybatis.domain.UserInfo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface UserDao {
    @Select({"select * from user where id = #{id}"})
    @Results(
            {
                    @Result(property = "id", column = "id"),
                    @Result(property = "username", column = "name"),
                    @Result(property = "gender", column = "gender"),
                    @Result(property = "birthday", column = "birthday"),
                    @Result(property = "address", column = "address")
            }
    )
    User findById(int id);

    @Insert("insert into user values(null, #{username}, #{gender}, #{birthday}, #{address})")
//    @SelectKey(statement = "select last_insert_id()", keyProperty = "id", resultType = Integer.class, before = false)
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void save(User user);

    @Update("update user set name = #{username} where id = #{id}")
    void updateUser(User user);

    @Update("update user set name = #{username} where id = #{id}")
    void updateUser2(@Param("id") int id, @Param("username") String name);

    @Delete("delete from user where id = #{id}")
    void deleteUser(int id);

    @Select(
            "<script>" +
            "select * from user " +
            "<where>\n" +
            "   <if test=\"username != null and username !='' \">\n" +
            "       and name = #{username}\n" +
            "   </if>\n" +

            "   <if test=\"address != null and address != ''\">\n" +
            "       and address = #{address}\n" +
            "   </if>\n" +

            "   <if test=\"gender != null and gender != ''\">\n" +
            "       and gender = #{gender}\n" +
            "   </if>\n" +
            "</where>"+
            "</script>"
    )
    @Results(
            {
                    @Result(property = "id", column = "id"),
                    @Result(property = "username", column = "name"),
                    @Result(property = "gender", column = "gender"),
                    @Result(property = "birthday", column = "birthday"),
                    @Result(property = "address", column = "address")
            }
    )
    List<User> findByCondition(User user);

    @Select(
            "<script>"+
                    "select * from user"+
                    "<where>\n" +
                    "   <foreach collection=\"list\" close=\")\" open=\"id in (\" item=\"i\" separator=\",\">\n" +
                    "       #{i}\n" +
                    "   </foreach>\n" +
                    "</where>"+
            "</script>"
    )
    @Results(
            {
                    @Result(property = "id", column = "id"),
                    @Result(property = "username", column = "name"),
                    @Result(property = "gender", column = "gender"),
                    @Result(property = "birthday", column = "birthday"),
                    @Result(property = "address", column = "address")
            }
    )
    List<User> findByIds(List<Integer> ids);

    @Select("select * from user where id = #{id}")
    @Results(
            {
                    @Result(property = "id", column = "id"),
                    @Result(property = "username", column = "name"),
                    @Result(property = "gender", column = "gender"),
                    @Result(property = "birthday", column = "birthday"),
                    @Result(property = "address", column = "address"),
                    @Result(property = "userInfo", column = "id", one = @One(
                            select = "com.mybatis.dao.UserDao.findByUid", fetchType = FetchType.LAZY
                    ))
            }
    )
    User findUserByIdWithInfo(int id);

    @Select("select * from user_info where uid = #{uid}")
    UserInfo findByUid(int uid);

    @SelectProvider(type = UserSql.class, method = "findByIds")
    List<User> findByIds2(@Param("ids") Integer[] ids);

    @Select("select * from user where id = #{id}")
    @Results(
            {
                    @Result(property = "id", column = "id"),
                    @Result(property = "username", column = "name"),
                    @Result(property = "gender", column = "gender"),
                    @Result(property = "birthday", column = "birthday"),
                    @Result(property = "address", column = "address"),
                    @Result(property = "orders", column = "id", many = @Many(
                            select = "com.mybatis.dao.OrderDao.findOrdersByUid", fetchType = FetchType.LAZY
                    ))
            }
    )
    List<User> findUsersWithOrder(int id);

    List<User> findUsersWithRole(int id);
}
